package com.atguigu.web.controller.ai;

import com.atguigu.common.core.domain.AjaxResult;
import com.atguigu.guli.ai.pojo.Chat;
import com.atguigu.guli.ai.pojo.Message;
import com.atguigu.guli.ai.service.AiService;
import com.atguigu.guli.ai.vo.ChatVo;
import com.atguigu.guli.ai.vo.MessageVo;
import com.atguigu.guli.ai.vo.QueryVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/ai")
public class AiController {
    @Resource
    private AiService aiService;

    /**
     * 创建会话
     * @param chatVo
     * @return
     */
    @PostMapping("/create-chat")
    public String createChat(@RequestBody ChatVo chatVo) {
        String chatId =  aiService.creareChat(chatVo);
        return chatId;
    }

    /**
     * 查询会话列表
     * @param chatVo
     * @return
     */
    @GetMapping("/list-chat")
    public List<Chat> getChat(ChatVo chatVo){
        List<Chat> chatList = aiService.getChat(chatVo);
        return chatList;
    }

    /**
     *修改会话标题
     * @param chatVo
     * @return
     */
    @PostMapping("/update-chat")
    public String updateChat(@RequestBody ChatVo chatVo){
        aiService.updateChat(chatVo);
        return "修改会话标题成功";
    }

    /**
     *  文本问答，保存聊天记录
     * @param queryVo
     * @return
     */
    @PostMapping(value = "/chat-stream",produces = "text/plain;charset=UTF-8")
    public Flux<String> chatStream(@RequestBody QueryVo queryVo){
        Flux<String> result = aiService.chatStream(queryVo);
        return result;
    }

    /**
     *  保存ai回复的消息到mongodb
     * @param messageVo
     * @return
     */
    @PostMapping("/save-msg")
    public AjaxResult saveMsg(@RequestBody MessageVo messageVo){
        aiService.saveMsg(messageVo);
        return AjaxResult.success();
    }

    @GetMapping("/list-msg")
    public List<Message> listMsg(Long chatId){
        List<Message> msgs = aiService.listMsg(chatId);
        return msgs;
    }
}
